package cn.edu.dgut.epidemic.controller;

import cn.edu.dgut.epidemic.beans.UserInfo;
import cn.edu.dgut.epidemic.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/user")
public class UserController {

    public static Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(UserInfo userInfo, Model model) {
        logger.debug("login() account:" + userInfo.getAccount() + ",密码:" + userInfo.getPassword());
        //通过业务逻辑层bean获取该账户对应的用户信息
        UserInfo user = this.userService.findByAccount(userInfo.getAccount());
        if (user == null) {
            //账号不正确
            model.addAttribute("msg", "账号不正确！");
            return "login";
        } else if (!user.getPassword().equals(userInfo.getPassword())) {
            //密码不正确
            model.addAttribute("msg", "密码不正确！");
            return "login";
        } else
            //密码正确，成功登录
            //使用重定向，避免数据多次传输
            return "redirect:/main.jsp";
    }
}
